<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: google_rss_feed.php$
* 	Letzter Stand:			$Revision: 172 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:55:02 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

include ('includes/application_top.php');

$Title = "Produkt Feed von ".HTTP_SERVER;
$Description = "Alle Produkte von ".HTTP_SERVER;
$copyright = HTTP_SERVER;
$SiteLink = HTTP_SERVER.DIR_WS_CATALOG;

if (GROUP_CHECK == 'true') 
	$group_check = " AND p.group_permission_".$_SESSION['customers_status']['customers_status_id']." = 1 ";

if ($_SESSION['customers_status']['customers_fsk18_display'] == '0') 
	$fsk_lock = ' AND p.products_fsk18!=1';

$listing = $db->db_query("SELECT
								p.products_id,
					            p.products_ean,
								pd.products_name,
					            pd.products_short_description,
					            pd.products_description,
								p.products_price,
								p.products_image,
								p.brand_id,
								p.products_weight,
					       		p.products_model,
					       		p.products_quantity,
					       		p.products_zustand,
					            p.group_permission_1,
								p.products_date_added,
								p.products_tax_class_id,
								s.specials_price_".$_SESSION['customers_status']['customers_status_id'].",
								UNIX_TIMESTAMP(s.expires_date),
								p2c.categories_id,
								gt.category_path
							FROM
								(".TABLE_PRODUCTS." p INNER JOIN ".TABLE_PRODUCTS_DESCRIPTION." pd ON p.products_id = pd.products_id)
								LEFT JOIN 
									".TABLE_SPECIALS." s ON p.products_id = s.products_id
								LEFT JOIN 
									".TABLE_PRODUCTS_TO_CATEGORIES." p2c ON p2c.products_id = p.products_id
								LEFT JOIN
									".TABLE_GOOGLE_TAXONOMY." gt ON gt.products_id = p.products_id
							WHERE
								p.products_status = '1'
							AND
								pd.language_id = '".(int)$_SESSION['languages_id']."'
							AND
								IF(s.specials_price_".$_SESSION['customers_status']['customers_status_id']." > 0, s.status = '1', '1')
								".$group_check.$fsk_lock."
							GROUP BY
								p.products_id
							ORDER BY
								p.products_date_added
							DESC");

require(DIR_WS_CLASSES.'class.order.php');
$order = new order;

$google_adress_data = $db->db_query(" SELECT 
											ab.entry_postcode,
											c.countries_id,
											c.countries_name,
											c.countries_iso_code_2,
											c.countries_iso_code_3,
											c.address_format_id
										FROM 
											".TABLE_ADDRESS_BOOK." ab 
											LEFT JOIN ".TABLE_COUNTRIES." AS c ON (c.countries_id = ab.entry_country_id)
										WHERE 
											ab.address_book_id = '1'");

      $order->customer = array('postcode' => $google_adress_data->fields['entry_postcode'],
                              'state' => $google_adress_data->fields['zone_name'],
                              'zone_id' => $google_adress_data->fields['zone_id'],
                              'country' => array('id' => $google_adress_data->fields['countries_id'],
			                              		 'title' => $google_adress_data->fields['countries_name'],
			                              		 'iso_code_2' => $google_adress_data->fields['countries_iso_code_2'],
			                              		 'iso_code_3' => $google_adress_data->fields['countries_iso_code_3'] ),
                              'format_id' => $google_adress_data->fields['address_format_id']);

      $order->delivery = array('postcode' => $google_adress_data->fields['entry_postcode'],
                              'state' => $google_adress_data->fields['zone_name'],
                              'zone_id' => $google_adress_data->fields['zone_id'],
                              'country' => array('id' => $google_adress_data->fields['countries_id'],
												'title' => $google_adress_data->fields['countries_name'],
												'iso_code_2' => $google_adress_data->fields['countries_iso_code_2'],
												'iso_code_3' => $google_adress_data->fields['countries_iso_code_3'] ),
                              'format_id' => $google_adress_data->fields['address_format_id']);

$_SESSION['delivery_zone'] = $google_adress_data->fields['countries_iso_code_2'];
$_SESSION['customer_country_id'] = $google_adress_data->fields['countries_id'];
$_SESSION['customer_zone_id'] = $google_adress_data->fields['zone_id'];

require_once(DIR_FS_INC.'inc.get_products_mo_images.php');
require_once(DIR_FS_INC.'inc.get_tax_rate.php');
require_once(DIR_FS_INC.'inc.get_category_path.php');
require_once(DIR_FS_INC.'inc.parse_category_path.php');
require_once(DIR_FS_INC.'inc.free_shipping.php');

require_once(DIR_WS_CLASSES.'class.price.php');
$price = new price(DEFAULT_CURRENCY, $_SESSION['customers_status']['customers_status_id']);

header("Content-type: text/xml; charset=utf-8");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<rss version=\"2.0\" xmlns:g=\"http://base.google.com/ns/1.0\" xmlns:c=\"http://base.google.com/cns/1.0\">\n";
echo "<channel>\n";
echo "\t<title>$Title</title>\n";
echo "\t<link>$SiteLink</link>\n";

require(DIR_WS_CLASSES.'class.shipping.php');

$suche = array('&', "'", '"', '#', "\n", '°');
$ersetze = array('&amp;', '', '', '', '', '');

while (!$listing->EOF) {

	$_SESSION['cart']->add_cart($listing->fields['products_id'], 1, '', false);
	
	echo "\n\t<item>\n";

	$products_name = str_replace($suche, $ersetze, $listing->fields['products_name']);

	if($listing->fields['products_description'] !='')
		$beschreibung = $listing->fields['products_description'];
	elseif($listing->fields['products_short_description'] !='')
		$beschreibung = $listing->fields['products_short_description'];
	else
		$beschreibung = $products_name;

	// Produktname
	echo "\t\t<title><![CDATA[".$products_name."]]></title>\n";

	// Link
	$link = href_link(FILENAME_PRODUCT_INFO, 'products_id='.$listing->fields['products_id']);
	echo "\t\t<link>".$link."</link>\n";

	// Beschreibung
	echo "\t\t<description><![CDATA[".$beschreibung."]]></description>\n";

	// Produkt ID
	echo "\t\t<guid>".$listing->fields['products_id']."</guid>\n";
	echo "\t\t<g:id>".$listing->fields['products_id']."</g:id>\n";
	
	$cat_path = get_product_path($listing->fields['products_id']);
	$cat_path = parse_category_path($cat_path);

	$path = '';
	for ($i = 0, $n = sizeof($cat_path); $i < $n; $i++) {
		$categories = $db->db_query("SELECT
											cd.categories_name
										FROM
											".TABLE_CATEGORIES_DESCRIPTION." cd,
											".TABLE_CATEGORIES." c
										WHERE
											cd.categories_id = '".$cat_path[$i]."'
										AND
											c.categories_id = cd.categories_id
										AND
											cd.language_id = '".(int)$_SESSION['languages_id']."'");

		if($categories->_numOfRows)
			$path .= ($i > 0 ? ' &gt; ' : '').str_replace($suche, $ersetze, $categories->fields['categories_name']);
		else
			break;
	}
	// Breadcrumb aka Produkttyp
	echo "\t\t<g:product_type><![CDATA[".$path."]]></g:product_type>\n";
	
	// Produkt Kategorie
	echo "\t\t<g:google_product_category>".(!empty($listing->fields['category_path']) ? $listing->fields['category_path'] : htmlspecialchars(GOOGLE_MERCHANT_GLOBAL))."</g:google_product_category>\n";

	// Sonderpeis
	if(!empty($listing->fields['specials_price_'.$_SESSION['customers_status']['customers_status_id']])) {
		echo "\t\t<g:sale_price>".trim($price->Format($listing->fields['specials_price_'.$_SESSION['customers_status']['customers_status_id']]))."</g:sale_price>\n";

		if(!empty($listing->fields['expire_date']))
			echo "\t\t<g:sale_price_effective_date>".date('c', time())."/".date('c', $listing->fields['expire_date'])."</g:sale_price_effective_date>\n";
	}
	// EAN Nummer
	if(!empty($listing->fields['products_ean']))
		echo "\t\t<g:gtin>".$listing->fields['products_ean']."</g:gtin>\n";

	// Gewicht
	echo "\t\t<g:shipping_weight>".(!empty($listing->fields['products_weight']) ? $listing->fields['products_weight'] : '0')." kg</g:shipping_weight>\n";

	// Herstellername
	if($listing->fields['brand_id'] > '0')
		$marke = $db->db_query("SELECT brand_name FROM ".TABLE_BRAND." WHERE brand_id = '".$listing->fields['brand_id']."'");
		
	echo "\t\t<g:brand>".(!empty($marke->fields['brand_name']) ? $marke->fields['brand_name'] : STORE_NAME)."</g:brand>\n";

	// Produktbilder, jedoch nicht mehr als insgesamt 10
	echo "\t\t<g:image_link>".(!empty($listing->fields['products_image']) ? HTTP_SERVER.DIR_WS_CATALOG.DIR_WS_ORIGINAL_IMAGES.$listing->fields['products_image'] : HTTP_SERVER.DIR_WS_CATALOG.DIR_WS_IMAGES.'no_img_info.jpg')."</g:image_link>\n";

	$images = get_products_mo_images($listing->fields['products_id']);
	if($images) {
		for($i = 0; $i < sizeof($images); $i++) {
			echo "\t\t<g:additional_image_link>".HTTP_SERVER.DIR_WS_CATALOG.DIR_WS_ORIGINAL_IMAGES.$images[$i]['image_name']."</g:additional_image_link>\n";
			if($i == 8)
		        break;
		}
	}
	
	// Alternativ zur MPN die Modellnummer bzw. veränderte Produkt ID
	echo "\t\t<g:mpn>".(!empty($listing->fields['products_model']) ? $listing->fields['products_model'] : 'mpn_'.$listing->fields['products_id'])."</g:mpn>\n";

	// Fertiger Produktpreis
	$google_price = $price->GetPrice($listing->fields['products_id'], false, 1, $listing->fields['products_tax_class_id'], $listing->fields['products_price']);
	echo "\t\t<g:price>".trim($price->format($google_price,true))."</g:price>\n";
	
	// auf Lager?
	if($listing->fields['products_quantity'] > 0)
		echo "\t\t<g:availability>auf lager</g:availability>\n";
	else
		echo "\t\t<g:availability>nicht auf lager</g:availability>\n";

	// Zustand
	echo "\t\t<g:condition>".$listing->fields['products_zustand']."</g:condition>\n";

	// Versandkosten
	$total_weight = $_SESSION['cart']->show_weight();
	$total_count = $_SESSION['cart']->count_contents();
	
	if(!is_object($shipping_modules))
		$shipping_modules = new shipping;
	
	$quotes = $shipping_modules->quote();
	
	if(!empty($quotes)) {
		$i = 1;
		foreach ($quotes AS $quote) {
			echo "\t\t<g:shipping>\n";
			echo "\t\t\t<g:country>DE</g:country>\n";
			echo "\t\t\t<g:service>".$quote['module']."</g:service>\n";
			echo "\t\t\t<g:price>".($quote['tax'] > 0 ? round(($quote['methods'][0]['cost'] * ( 100 + $quote['tax'] ) / 100),2) : $quote['methods'][0]['cost'])."</g:price>\n";
			echo "\t\t</g:shipping>\n";
			if($i==10)
				break;
			$i++;
		}
	} else {
		echo "\t\t<g:shipping>\n";
		echo "\t\t\t<g:country>DE</g:country>\n";
		echo "\t\t\t<g:service>kostenlos</g:service>\n";
		echo "\t\t\t<g:price>0</g:price>\n";
		echo "\t\t</g:shipping>\n";
	}
	echo "\t</item>\n";
	flush();
	$_SESSION['cart']->reset();
	$listing->MoveNext();
}
echo "</channel>\n";
echo "</rss>";
$_SESSION['cart']->reset(true);
unset($_SESSION['cart']);
